

package com.mt.restaurant.controller.sale;

import com.mt.restaurant.entity.sale.MenuSaleRecord;
import org.apache.ibatis.annotations.Param;
import org.springframework.security.access.prepost.PreAuthorize;
import com.mt.common.core.annotation.ApiPageParam;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import com.mt.common.core.web.base.PageDTO;
import com.mt.common.core.web.base.PageResultDTO;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;


import com.mt.restaurant.dto.sale.SaleOrderSettlementEditDto;
import com.mt.restaurant.entity.sale.SaleOrderSettlement;
import com.mt.restaurant.service.sale.SaleOrderSettlementService;
import com.mt.restaurant.service.sale.SaleOrderService;


@Api(tags = "销售订单结算")
@RestController
@RequestMapping("/api/restaurant/sale/SaleOrderSettlement")
@CrossOrigin(allowCredentials = "true")
public class SaleOrderSettlementController {
    private static Logger logger = LoggerFactory.getLogger(SaleOrderSettlementController.class);


    @Autowired
    private SaleOrderSettlementService saleOrderSettlementService;
    @Autowired
    private SaleOrderService saleOrderService;

    /**
     * 根据分页参数查询销售订单结算集合
     *
     * @param pageDTO 分页条件
     */
    @PreAuthorize("hasAuthority('restaurant:sale:SaleOrderSettlement:view')")
    @ApiOperation("分页查询销售订单结算")
    @ApiPageParam
    @PostMapping("/findSaleOrderSettlements")
    public PageResultDTO findSaleOrderSettlements(@RequestBody PageDTO pageDTO) {
        return this.saleOrderSettlementService.findSaleOrderSettlements(pageDTO);
    }

    /**
     * 根据ID查询指定的销售订单结算
     *
     * @param saleOrderSettlementId Id
     */
    @PreAuthorize("hasAuthority('restaurant:sale:SaleOrderSettlement:view')")
    @ApiOperation("根据id查询销售订单结算")
    @ApiPageParam
    @PostMapping("/findSaleOrderSettlement")
    public SaleOrderSettlement findSaleOrderSettlement(@RequestParam Long saleOrderSettlementId) {
        return this.saleOrderSettlementService.findSaleOrderSettlement(saleOrderSettlementId);
    }

    /**
     * 根据ID查询指定的销售订单结算(包含外键名称)
     *
     * @param saleOrderSettlementId Id
     */
    @PreAuthorize("hasAuthority('restaurant:sale:SaleOrderSettlement:view')")
    @ApiOperation("根据ID查询指定的销售订单结算(包含外键名称)")
    @PostMapping("/findSaleOrderSettlementForView")
    public SaleOrderSettlement findSaleOrderSettlementForView(@RequestParam Long saleOrderSettlementId) {
        return this.saleOrderSettlementService.findSaleOrderSettlementWithForeignName(saleOrderSettlementId);
    }

    /**
     * 根据ID查询指定的销售订单结算(包含销售订单结算和外键名称)
     *
     * @param saleOrderSettlementId Id
     */
    @PreAuthorize("hasAuthority('restaurant:sale:SaleOrderSettlement:view')")
    @ApiOperation("根据ID查询指定的销售订单结算(包含销售订单结算和外键名称)")
    @PostMapping("/findSaleOrderSettlementForEdit")
    public SaleOrderSettlementEditDto findSaleOrderSettlementForEdit(@RequestParam Long saleOrderSettlementId) {
        SaleOrderSettlementEditDto saleOrderSettlementEditDto = new SaleOrderSettlementEditDto();
        saleOrderSettlementEditDto.setSaleOrderSettlement(this.saleOrderSettlementService.findSaleOrderSettlementWithForeignName(saleOrderSettlementId));

        this.prepareSaleOrderSettlementEditDto(saleOrderSettlementEditDto);

        return saleOrderSettlementEditDto;
    }

    /**
     * 根据ID查询指定的销售订单结算(只提取ID 和 Name)
     *
     * @param saleOrderSettlementId Id
     */
    @PreAuthorize("hasAuthority('restaurant:sale:SaleOrderSettlement:view')")
    @ApiOperation("根据ID查询指定的销售订单结算(只提取ID 和 Name)")
    @PostMapping("/findSaleOrderSettlementsWithIdNameById")
    public SaleOrderSettlement findSaleOrderSettlementsWithIdNameById(@RequestParam Long saleOrderSettlementId) {
        return this.saleOrderSettlementService.findSaleOrderSettlementsWithIdNameById(saleOrderSettlementId);
    }

    /**
     * 根据名称查询销售订单结算集合(只提取ID 和 Name)
     *
     * @param saleOrderSettlementName 名称
     */
    @PreAuthorize("hasAuthority('restaurant:sale:SaleOrderSettlement:view')")
    @ApiOperation("根据名称查询销售订单结算集合(只提取ID 和 Name)")
    @PostMapping("/findSaleOrderSettlementsWithIdNameByName")
    public List<SaleOrderSettlement> findSaleOrderSettlementsWithIdNameByName(@RequestParam String saleOrderSettlementName) {
        return this.saleOrderSettlementService.findSaleOrderSettlementsWithIdNameByName(saleOrderSettlementName);
    }


    /**
     * 创建新的销售订单结算
     */
    @PreAuthorize("hasAuthority('restaurant:sale:SaleOrderSettlement:add')")
    @ApiOperation("创建新的销售订单结算")
    @PostMapping("/createSaleOrderSettlement")
    public SaleOrderSettlementEditDto createSaleOrderSettlement() {
        SaleOrderSettlementEditDto saleOrderSettlementEditDto = new SaleOrderSettlementEditDto();
        saleOrderSettlementEditDto.setSaleOrderSettlement(new SaleOrderSettlement());

        this.prepareSaleOrderSettlementEditDto(saleOrderSettlementEditDto);
        return saleOrderSettlementEditDto;
    }

    private void prepareSaleOrderSettlementEditDto(SaleOrderSettlementEditDto saleOrderSettlementEditDto) {
        //TODO: 以下代码可以注释掉，此行代码即时加载所有外键对象，以便选择。如果不在此加载，可以在界面上实现延迟加载。如果外键对象超过 500 行，建议采用延迟加载
        saleOrderSettlementEditDto.setSaleOrderSaleOrders(this.saleOrderService.findAllSaleOrdersWithIdName());
    }

    /**
     * 新增保存销售订单结算
     *
     * @param saleOrderSettlement 实体对象
     */
    @PreAuthorize("hasAuthority('restaurant:sale:SaleOrderSettlement:add')")
    @ApiOperation("新增保存销售订单结算")
    @PostMapping("/saveSaleOrderSettlement")
    public SaleOrderSettlement saveSaleOrderSettlement(@RequestBody SaleOrderSettlement saleOrderSettlement) {
        return this.saleOrderSettlementService.saveSaleOrderSettlement(saleOrderSettlement);
    }

    /**
     * 修改保存销售订单结算
     *
     * @param saleOrderSettlement 实体对象
     */
    @PreAuthorize("hasAuthority('restaurant:sale:SaleOrderSettlement:update')")
    @ApiOperation("修改保存销售订单结算")
    @PostMapping("/updateSaleOrderSettlement")
    public SaleOrderSettlement updateSaleOrderSettlement(@RequestBody SaleOrderSettlement saleOrderSettlement) {
        return this.saleOrderSettlementService.updateSaleOrderSettlement(saleOrderSettlement);
    }

    /**
     * 根据ID删除销售订单结算
     *
     * @param saleOrderSettlementId ID
     */
    @PreAuthorize("hasAuthority('restaurant:sale:SaleOrderSettlement:remove')")
    @ApiOperation("根据ID删除销售订单结算")
    @PostMapping("/deleteSaleOrderSettlement")
    public void deleteSaleOrderSettlement(@RequestParam Long saleOrderSettlementId) {
        this.saleOrderSettlementService.deleteSaleOrderSettlement(saleOrderSettlementId);
    }

    @ApiOperation("根据订单Id获取菜品信息")
    @PostMapping("/getMenuSaleRecordsByOrderId")
    public List<MenuSaleRecord> getMenuSaleRecordsByOrderId(@Param("orderId") Long orderId) {
      return  this.saleOrderSettlementService.getMenuSaleRecordsByOrderId(orderId);
    }

}

